package com.iemes.controller.quality;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.base.baseExtController;
import com.iemes.entity.SlakingCheckFormMap;
import com.iemes.mapper.quality.SlakingCheckMapper;
import com.iemes.plugin.PageView;
import com.iemes.upload.UploadUtil;
import com.iemes.util.Common;

@RequestMapping("/quality")
@Controller
public class SlakingCheckController extends baseExtController {

	@Inject
	private SlakingCheckMapper slakingCheckMapper;
	
	
	@RequestMapping("slakingcheck")
	private String fine(Model model){
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/quality/slakingcheck/slakingcheck_list";
	}
	
	@ResponseBody
	@RequestMapping("slakingcheck_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception 
	{
		SlakingCheckFormMap slakingCheckFormMap=getFormMap(SlakingCheckFormMap.class);
		List<Map<String,Object>> slakingCheckFormMaps=slakingCheckMapper.getSlakingCheckFormMapByCondtion(slakingCheckFormMap);
		for(Map<String,Object>map:slakingCheckFormMaps){
			if(map.get("confirmationOf").toString().equals("1")){
				map.put("confirmationOf", "甲班");
			}
			if(map.get("confirmationOf").toString().equals("2")){
				map.put("confirmationOf", "乙班");
			}
			if(map.get("qualified").toString().equals("1")){
				map.put("qualified", "合格");
			}
			if(map.get("qualified").toString().equals("2")){
				map.put("qualified", "不合格");
			}
		}
		LuogicPaging(pageNow, pageSize, slakingCheckFormMaps);
		return pageView;
	}
	
	
	@ResponseBody
	@RequestMapping(value="slakingcheck_deleteEntity")
	@SystemLog(module="熟化笼检测",methods="熟化笼检测-删除")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		SlakingCheckFormMap slakingCheckFormMap=getFormMap(SlakingCheckFormMap.class);
		String[] ids = getParaValues("ids");
		String arr=ids[0];
		String[] str=arr.split(",");
		for(int i=0;i<str.length;i++){
			slakingCheckFormMap.put("state", 1);
			slakingCheckFormMap.put("id", str[i]);
			baseMapper.editEntity(slakingCheckFormMap);
		}
		return "success";
	}
	
	
	@RequestMapping("slakingcheck_addUI")
	public String addUI(Model model) throws Exception {
		return Common.BACKGROUND_PATH + "/quality/slakingcheck/slakingcheck_add";
	}
	
	
	@ResponseBody
	@RequestMapping("slakingcheck_addEntity")
//	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="熟化笼检测",methods="熟化笼检测-新增物料")//凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity(MultipartFile appearancePic,Model model,HttpServletRequest request) throws Exception {
		SlakingCheckFormMap slakingCheckFormMap=getFormMap(SlakingCheckFormMap.class);
		
		//文件上传
		// 图片新名字
        String newName = UUID.randomUUID().toString();
        // 图片原来的名字
        String oldName = appearancePic.getOriginalFilename();
        // 后缀
        String sux = oldName.substring(oldName.lastIndexOf("."));
         //新建本地文件流
        File file = new File("E:\\ty-project\\TY-ERP\\src\\main\\webapp\\images\\" + newName + sux);
        // 写入本地磁盘
        appearancePic.transferTo(file);

        System.out.println("newName:"+newName);
		System.out.println("sux:"+sux);
		String picNmae="<img onclick=\"ok(this)\" height=\"30\" width=\"30\" src=/TY-ERP/images/"+newName+sux + "\"/>";
		System.out.println(appearancePic.getOriginalFilename());
		/*if (!appearancePic.isEmpty()) {
			// 类型的验证
			// 原文件名（包含后缀名）
			String originalFilename = appearancePic.getOriginalFilename();
			// 后缀名
			String getUploadContentType = originalFilename
					.substring(originalFilename.lastIndexOf(".") + 1);
			UploadUtil uploadUtil = new UploadUtil();
			uploadUtil.uploadImage2(request, appearancePic, getUploadContentType,
					originalFilename);
			String imagePath2 = uploadUtil.getImagePath2();
			String imageSrc = "<img onclick=\"ok(this)\" height=\"30\" width=\"30\" src=\"http://localhost:8080/TY-ERP"+imagePath2 + "\"/>";
		
			String regexp = "\'";
			imagePath2=imagePath2.replace(regexp,  "\"");
			slakingCheckFormMap.set("appearancePic", imageSrc);

			System.out.println("你好吗"+imageSrc);
		}*/
		
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		String productName=request.getParameter("slakingCheckFormMap.productName");
		String specification=request.getParameter("slakingCheckFormMap.specification");
		String curingTime=request.getParameter("slakingCheckFormMap.curingTime");
		String weight=request.getParameter("slakingCheckFormMap.weight");
		String actualMeasurement=request.getParameter("slakingCheckFormMap.actualMeasurement");
		String appearance=request.getParameter("slakingCheckFormMap.appearance");
		String qualified=request.getParameter("slakingCheckFormMap.qualified");
		String exceptionHandling=request.getParameter("slakingCheckFormMap.exceptionHandling");
		String acknowledgingTime=request.getParameter("slakingCheckFormMap.acknowledgingTime");
		String confirmationOf=request.getParameter("slakingCheckFormMap.confirmationOf");
		slakingCheckFormMap.put("checkTime", sdf.format(date));
		slakingCheckFormMap.put("appearancePic",picNmae);
		slakingCheckFormMap.put("state", 0);
		slakingCheckFormMap.put("productName", productName);
		slakingCheckFormMap.put("specification", specification);
		slakingCheckFormMap.put("curingTime", curingTime);
		slakingCheckFormMap.put("weight", weight);
		slakingCheckFormMap.put("actualMeasurement", actualMeasurement);
		slakingCheckFormMap.put("appearance", appearance);
		slakingCheckFormMap.put("qualified", qualified);
		slakingCheckFormMap.put("exceptionHandling", exceptionHandling);
		slakingCheckFormMap.put("acknowledgingTime", acknowledgingTime);
		slakingCheckFormMap.put("confirmationOf", confirmationOf);
		try {
			baseMapper.addEntity(slakingCheckFormMap);
		} catch (Exception e) {
			e.printStackTrace();
			return "false";
		}
		return "success";
	}
	
	
	@RequestMapping("slakingcheck_edit")
	public String editUI(Model model) throws Exception 
	{
		String id = getPara("id");
		SlakingCheckFormMap slakingCheckFormMap=slakingCheckMapper.getSlakingCheckFormMapById(Integer.parseInt(id));
		model.addAttribute("slakingCheckFormMap", slakingCheckFormMap);
		return Common.BACKGROUND_PATH + "/quality/slakingcheck/slakingcheck_edit";
	}
	
	

	@ResponseBody
	@RequestMapping(value="slakingcheck_editEntity")
	@SystemLog(module="熟化笼检测",methods="熟化笼检测-修改")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		System.out.println("5444564");
		SlakingCheckFormMap slakingCheckFormMap=getFormMap(SlakingCheckFormMap.class);
		baseMapper.editEntity(slakingCheckFormMap);
		return "success";
	}
	
}
